<template>
    <d2-container>
        <!-- 搜索表头 -->
        <template slot="header">
            <el-form :inline="true" :model="searchForm" ref="searchForm" size="small" v-if="showHeader" class="searchHeader">

                <el-form-item label="表名" prop="tableName">
                    <el-input v-model="searchForm.tableName" placeholder="请输入名称" clearable />
                </el-form-item>
                <el-form-item label="描述" prop="tableDesc">
                    <el-input v-model="searchForm.tableDesc" placeholder="请输入名称" clearable />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="getDataTable">
                        <d2-icon name="search" />
                        查询
                    </el-button>
                    <el-button @click="handleFormReset">
                        <d2-icon name="refresh" />
                        重置
                    </el-button>
                </el-form-item>

            </el-form>
        </template>

        <!-- 表格表头 -->
        <div class="table-header">
            <div></div>
            <div>
                <ec-prompt-button content="刷新" icon="el-icon-refresh" @click="getDataTable()" v-if="isAuth()"></ec-prompt-button>
                <ec-prompt-button content="搜索" icon="el-icon-search" @click="changeHeader()" v-if="isAuth()"></ec-prompt-button>
                <ec-prompt-button content="导入" icon="el-icon-download"></ec-prompt-button>
                <ec-prompt-button content="导出" icon="el-icon-upload2"></ec-prompt-button>
                <ec-prompt-button content="分享" icon="el-icon-share"></ec-prompt-button>
            </div>
        </div>


        <!-- 表格表体 -->
        <el-table :data="tabledata" size="small" min-height="300" style="width: 100%" border highlight-current-row
            :header-cell-style="{'background':'#fcfcfc','text-align':'center'}"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" header-align="center" align="center" width="50">
            </el-table-column>
            <el-table-column prop="pkTable" header-align="center" align="center" show-overflow-tooltip label="表主键">
            </el-table-column>
            <el-table-column prop="tableName" header-align="center" align="center" show-overflow-tooltip label="表名">
            </el-table-column>
            <el-table-column prop="tableDesc" header-align="center" align="center" show-overflow-tooltip label="表描述">
            </el-table-column>
            <el-table-column prop="version" header-align="center" align="center" show-overflow-tooltip label="表版本">
            </el-table-column>
            <el-table-column prop="createdByName" header-align="center" align="center" show-overflow-tooltip
                label="创建人">
            </el-table-column>
            <el-table-column prop="createdTime" header-align="center" align="center" show-overflow-tooltip label="创建时间">
            </el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" width="120" label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="managerHandle(scope.row.pkTable)"
                        class="el-icon-setting" v-if="isAuth()">管理
                    </el-button>
                    <el-button type="text" size="small" @click="deleteHandle(scope.row.pkTable)" class="el-icon-delete"
                        v-if="isAuth()">删除
                    </el-button>
                </template>
            </el-table-column>

            <div slot="empty">
                <div class="avue-empty">
                    <div class="avue-empty__image" style="height: 50px;"><img
                            src=""
                            alt=""></div>
                    <p class="avue-empty__desc">暂无数据</p>
                </div>
            </div>
        </el-table>

        <!-- 表格表尾 -->
        <template slot="footer">
            <!-- <el-button type="primary" size="mini" :loading="uploading" style="float:left;">
                <d2-icon name="cloud-upload" />
                Upload log data
            </el-button> -->

            <ec-pagination :page="page" @size-change="handleSizeChange" @current-change="handleCurrentChange">
            </ec-pagination>
        </template>

        <!-- 管理 -->
        <manage v-if="manageVisible" ref="manage" @refreshDataList="getDataTable"></manage>
    </d2-container>
</template>

<script>
    import * as tableinfoService from '@/api/bdms/tableinfo'

    import Manage from './manage'

    export default {
        data() {
            return {
                searchForm: {
                    tableName: '',
                    tableDesc: '',
                },
                page: {
                    page: 1,
                    limit: 10,
                    total: 0
                },
                showHeader: true,
                tabledata: [],
                multipleSelection: [],
                loading: false,
                addOrUpdateVisible: false,
                manageVisible: false,
            }
        },
        components: {
            Manage
        },
        mounted() {
            this.getDataTable()
        },
        methods: {
            // 获取数据列表
            getDataTable() {
                this.loading = true
                let query = {
                    page: this.page.page,
                    limit: this.page.limit,
                    tableName: this.searchForm.tableName,
                    tableDesc: this.searchForm.tableDesc
                };
                tableinfoService.gettableinfopage(query).then(data => {
                    this.tabledata = data.list;
                    this.page.total = data.totalCount;
                    this.loading = false;
                }).catch((res) => {
                    this.loading = false;
                });
            },
            //切换页数
            handleSizeChange(val) {
                this.page.limit = val;
                this.getDataTable();
            },
            // 翻页
            handleCurrentChange(val) {
                this.page.page = val;
                this.getDataTable();
            },
            // 切换显示搜索表头
            changeHeader() {
                this.showHeader = !this.showHeader
            },
            // 重置搜索表单
            handleFormReset() {
                this.$refs.searchForm.resetFields()
            },
            // 新增 / 修改
            addOrUpdate(id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            },
            managerHandle(id) {
                this.manageVisible = true
                this.$nextTick(() => {
                    this.$refs.manage.init(id)
                })
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 删除
            deleteHandle(id) {
                if (id) {
                    this.$confirm("确认删除？", "确认信息", {
                        distinguishCancelAndClose: true,
                        confirmButtonText: "删除",
                        cancelButtonText: "取消"
                    }).then(() => {
                        tableinfoService.deltableinfo(id).then(() => {
                            this.getDataTable();
                        });
                    });
                }
            }
        }
    }
</script>